package com.sumer.brain.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sumer.brain.sys.model.entity.SysRole;
import com.sumer.brain.sys.service.SysRoleService;
import com.sumer.brain.sys.mapper.SysRoleMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@RequiredArgsConstructor
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    private final SysRoleMapper sysRoleMapper;

    @Override
    public List<SysRole> getRolesByUserId(Long userId) {
        return sysRoleMapper.getRolesByUserId(userId);
    }

    @Override
    public int saveRoleMenu(SysRole sysRole) {
        return sysRoleMapper.saveRoleMenu(sysRole);
    }

    @Override
    public Boolean delByIds(String ids) {
        // 删除角色
        List<String> roleIds = Arrays.asList(ids.split(","));
        sysRoleMapper.deleteBatchIds(roleIds);
        // 删除角色菜单关联表数据
        sysRoleMapper.delRoleMenuByRoleIds(roleIds);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRoleMenu(SysRole sysRole) {
        sysRoleMapper.deleteRoleMenu(sysRole);
        sysRoleMapper.saveRoleMenu(sysRole);
    }
}




